package com.czd.support.controller;

import com.czd.support.model.Links;
import com.czd.support.model.Manager;
import com.czd.support.service.LinksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * 功能说明：友情链接管理控制器
 * 作者：liuxing(2014-12-20 22:52)
 */
@Controller
@RequestMapping(value = "/links")
public class LinksController extends BaseController {

    @Autowired
    private LinksService linksService;

    /**
     * 友情链接管理列表
     * @return
     */
    @RequestMapping(value = {"", "/", "list"}, method = RequestMethod.GET)
    public String list(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                       Model model) {
        model.addAttribute("paginate", linksService.queryForPaginate(pageNo));
        return "links/list";
    }

    /**
     * 友情链接详情页
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}/details", method = RequestMethod.GET)
    public String details(@PathVariable("id") int id, Model model) {
        Links links = linksService.findById(id);
        if (links == null) {
            return error(model, "信息不存在", "链接不存在");
        }

        model.addAttribute("links", links);
        return "links/details";
    }

    /**
     * 进入新增友情链接
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() {
        return "links/add";
    }

    /**
     * 保存新增
     * @param name
     * @param link
     * @param tank
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String save(@RequestParam("name") String name,
                       @RequestParam("link") String link,
                       @RequestParam("tank") int tank,
                       HttpServletRequest request, Model model) {
        Manager manager = getSesionUser(request);
        boolean insertFlag = linksService.insert(name, link, tank, manager.getUserCode(), manager.getUserCode());
        if (insertFlag) {
            return "redirect:/links/list";
        }

        return error(model, "保存失败", "保存失败");
    }

    /**
     * 进入编辑友情链接
     * @return
     */
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public String edit(@PathVariable("id") int id, Model model) {
        Links links = linksService.findById(id);
        if (links == null) {
            return error(model, "信息不存在", "链接不存在");
        }

        model.addAttribute("links", links);
        return "links/edit";
    }

    /**
     * 保存编辑
     * @param id
     * @param name
     * @param link
     * @param tank
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.POST)
    public String save(@PathVariable("id") int id,
                       @RequestParam("name") String name,
                       @RequestParam("link") String link,
                       @RequestParam("tank") int tank,
                       HttpServletRequest request, Model model) {
        Manager manager = getSesionUser(request);
        boolean updateFlag = linksService.update(id, name, link, tank, manager.getUserCode());
        if (updateFlag) {
            return "redirect:/links/list";
        }

        return error(model, "保存失败", "保存失败");
    }

}
